package com.mt.api.modules.controller;

import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.log.annotation.Log;
import com.mt.api.modules.domain.bo.CustomerAddBo;
import com.mt.api.modules.domain.bo.CustomerDelBo;
import com.mt.api.modules.domain.bo.CustomerListBo;
import com.mt.api.modules.domain.bo.CustomerPageBo;
import com.mt.api.modules.domain.vo.CustomerInfoVo;
import com.mt.api.modules.service.AppCustomerService;
import com.mt.api.util.ParamsValid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * @author plover
 * @date 2020-07-06
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "客户管理")
@RequestMapping("/app/customer")
public class AppCustomerController {

    private final AppCustomerService customerService;

    @PostMapping("/add")
    public WebResponse addCustomer(@RequestBody CustomerAddBo bo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("收货人不能为空", bo.getReceiver())
                .validNotNull("收件人地区", bo.getRarea());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return customerService.addCustomer(bo);
    }

    @PostMapping("/edit")
    public WebResponse editCustomer(@RequestBody CustomerAddBo bo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("收货人不能为空", bo.getReceiver())
                .validNotNull("id不能为空", bo.getId());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return customerService.editCustomer(bo);
    }

    @PostMapping("/del")
    public WebResponse delCustomer(@RequestBody CustomerDelBo bo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("id不能为空", bo.getId());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return customerService.delCustomer(bo);
    }

    @GetMapping("/info")
    @ApiOperation(value = "", notes = "客户信息接口", httpMethod = "GET")
    public WebResponse<CustomerInfoVo> getCustomerInfo(Integer id) {
        return customerService.getCustomerInfo(id);
    }

    @GetMapping("/list")
    @Log("查询品牌下的所有客户")
    @ApiOperation(value = "", notes = "客户信息列表", httpMethod = "GET")
    public WebResponse listByBrandId(CustomerListBo customerListBo) {
        return customerService.listByBrandId(customerListBo);
    }

    @GetMapping("/page")
    @ApiOperation(value = "", notes = "客户信息分页列表", httpMethod = "GET")
    public WebResponse pageByBrandId(CustomerPageBo customerPageBo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("请输入页码", customerPageBo.getPageNum())
                .validNotNull("请输入页尺寸", customerPageBo.getPageSize());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return customerService.pageByBrandId(customerPageBo);
    }

    @GetMapping("/getCustomerStatPage")
    @Log("客户统计分页列表")
    @ApiOperation(value = "", notes = "客户统计分页列表", httpMethod = "GET")
    public WebResponse getCustomerStatPage(CustomerPageBo customerPageBo) {
        return customerService.getCustomerStatPage(customerPageBo);
    }
}